package com.sy.service.impl;


import com.alibaba.fastjson.JSON;
import com.sy.dto.Params;
import com.sy.dto.TrainerDto;
import com.sy.dto.TrainersDto;
import com.sy.entity.*;
import com.sy.service.TrainerService;
import com.sy.utils.ParamsUtils;
import com.sy.utils.RedisOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class TrainerServiceImpl extends BaseServiceImpl implements TrainerService {
    @Autowired
    private RedisOperations redisOperations;
    /**
     * 通过条件查询教练的详细集合
     * @param params
     * @return
     */
    @Override
    public List<TrainersDto> getTrainerByParams(Params params) {
        List<TrainersDto> trainersDtoList;
        String key = "TrainerList" + ParamsUtils.add(params);
        if (redisOperations.exists(key)) {
            System.out.println("从Redis中直接获取...");
            trainersDtoList = JSON.parseArray(redisOperations.get(key), TrainersDto.class);
        } else {
            List<TrainersDto> trainersDtoList1 = new ArrayList<>();
            PriceRegion priceRegion = new PriceRegion();
            String sort = "";
            //获取价格区间
            if (!Objects.isNull(params.getPrice())) {
                priceRegion = priceRegionMapper.getPriceRegionById(params.getPrice());
                System.out.println(priceRegion.toString());
            }
            //获取排序方式
            if (!Objects.isNull(params.getSortType())) {
                System.out.println(String.valueOf(params.getSortType()).trim());
                sort = (params.getSortType() == 1) ? "count" : (params.getSortType() == 2 ? "price" : "count");
                System.out.println(sort);
            }
            List<Province> provinces = new ArrayList<>();
            List<DriSchool> driSchoolList = new ArrayList<>();
            //通过城市id获得该城市及区域内的所有区域集合
            if (!Objects.isNull(params.getPid())) {
                Province provinceByPid = provinceMapper.getProvinceByPid(params.getPid());
                provinces.add(provinceByPid);
                if (params.getPid() < 32) {
                    provinceMapper.getProvincesByProvinceId(params.getPid()).forEach(province -> provinces.add(province));
                }
            }
            //获取所有城市驾校信息
            List<List<DriSchool>> collect = provinces.stream().map(province -> driSchoolMapper.getDriSchoolByLocId(province.getPid())).collect(Collectors.toList());
            collect.forEach(driSchools -> driSchools.forEach(driSchool -> driSchoolList.add(driSchool)));
            String finalSort = sort;
            PriceRegion finalPriceRegion = priceRegion;
//            System.out.println(finalPriceRegion.toString());
            //分页时跳过的数据条数
            Integer start = (params.getPage() - 1) * 10;
            //取得驾校id的集合
            List<Integer> driSchoolIds = driSchoolList.stream().map(driSchool -> driSchool.getDriSchoolId()).collect(Collectors.toList());
            if (!Objects.isNull(params.getPid()) & driSchoolIds.size() == 0) {
                driSchoolIds.add(-1);
            }
            //获取教练的集合
            List<Trainer> trainerByDriId = trainerMapper.getTrainerByDriId(driSchoolIds, params.getKeyWord(), finalSort, params.getIsSort(), finalPriceRegion.getStartPrice(), finalPriceRegion.getEndPrice(), start);
            List<TrainersDto> collect1 = trainerByDriId.stream().map(trainer -> {
                TrainersDto trainersDto = new TrainersDto();
                DriSchool driSchoolById = driSchoolMapper.getDriSchoolById(trainer.getDriId());
                trainersDto.setDriSchoolId(driSchoolById.getDriSchoolId());
                trainersDto.setDriName(driSchoolById.getDriName());
                trainersDto.setTrainer(trainer);
                return trainersDto;
            }).collect(Collectors.toList());
            trainersDtoList1.addAll(collect1);
            trainersDtoList = trainersDtoList1;
            //存入redis中
            redisOperations.set(key, JSON.toJSONString(trainersDtoList));
        }
        return trainersDtoList;
    }

    /**
     * 通过教练id查询教练的详细信息
     * @param id
     * @return
     */
    @Override
    public TrainerDto getTrainerById(Integer id) {
        TrainerDto trainerDto;
            List<Commit> commitList = commitMapper.getCommitListByDrId(id,2);
            List<TechTime> techTimeListByTid = techTimeMapper.getTechTimeListByTid(id);
            Trainer trainerByTrainerId = trainerMapper.getTrainerByTrainerId(id);
            DriSchool driSchoolById = driSchoolMapper.getDriSchoolById(trainerByTrainerId.getDriId());
            trainerDto = new TrainerDto(driSchoolById.getDriSchoolId(), driSchoolById.getDriName(), trainerByTrainerId, techTimeListByTid, commitList);
        return trainerDto;
    }

    /**
     * 通过条件查询当前条件下的教练信息总页码数
     * @param params
     * @return
     */
    @Override
    public Integer getCountByParams(Params params) {
        Integer count;
            PriceRegion priceRegion = new PriceRegion();
            String sort = "";
            //获取价格区间
            if (!Objects.isNull(params.getPrice())) {
                priceRegion = priceRegionMapper.getPriceRegionById(params.getPrice());
                System.out.println(priceRegion.toString());
            }
            //获取排序方式
            if (!Objects.isNull(params.getSortType())) {
                System.out.println(String.valueOf(params.getSortType()).trim());
                sort = (params.getSortType() == 1) ? "count" : (params.getSortType() == 2 ? "price" : "count");
                System.out.println(sort);
            }
            List<Province> provinces = new ArrayList<>();
            List<DriSchool> driSchoolList = new ArrayList<>();
            //通过城市id获得该城市及区域内的所有区域集合
            if (!Objects.isNull(params.getPid())) {
                Province provinceByPid = provinceMapper.getProvinceByPid(params.getPid());
                provinces.add(provinceByPid);
                if (params.getPid() < 32) {
                    provinceMapper.getProvincesByProvinceId(params.getPid()).forEach(province -> provinces.add(province));
                }
            }
            //获取所有城市驾校信息
            List<List<DriSchool>> collect = provinces.stream().map(province -> driSchoolMapper.getDriSchoolByLocId(province.getPid())).collect(Collectors.toList());
            collect.forEach(driSchools -> driSchoolList.addAll(driSchools));
            String finalSort = sort;
            PriceRegion finalPriceRegion = priceRegion;
//            System.out.println(finalPriceRegion.toString());
            //取得驾校id的集合
            List<Integer> driSchoolIds = driSchoolList.stream().map(driSchool -> driSchool.getDriSchoolId()).collect(Collectors.toList());
            //在当前条件下查询教练的数量
            Integer count1 = (int) trainerMapper.getCountByDriId(driSchoolIds, params.getKeyWord(), finalSort, params.getIsSort(), finalPriceRegion.getStartPrice(), finalPriceRegion.getEndPrice());
        count = (count1 / 10);
        if(10*count<count1 & count>0){
            count=count+1;
        }
        return count;

    }
}
